#ifndef DPGRAPHICSITEM_H
#define DPGRAPHICSITEM_H


#include <QtGui/QtGui>
#include <QtCore/QtCore>

class QAbstractTransition;

Q_DECLARE_METATYPE(QPixmap*);

enum AnimType
{
    ANIM_Normal,
    ANIM_Left,
    ANIM_Right,
    ANIM_Up,
    ANIM_Down,
    ANIM_LeftBack,
    ANIM_RightBack,
    ANIM_UpBack,
    ANIM_DownBack,
    ANIM_Disappear,
};

class DPGraphicsItem : public QGraphicsWidget
{
    Q_OBJECT
public:
    DPGraphicsItem( int pixIndex, QGraphicsItem * parent = 0 );

    virtual ~DPGraphicsItem(){}

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *);

    QRectF boundingRect() const;

    int      getPixmapIndex() const { return mPixmapIndex; }
    void     setPixmapIndex(int pixmapIndex) { mPixmapIndex = pixmapIndex; }
    void     setWillChangeIndex(int willChangeIndex) { mWillChangeIndex = willChangeIndex; }


    QPoint   getCoord() { return mCoord; }
    void     setCoord(const QPoint& coord);
    void     anim(const AnimType& animType);
    void     fallRows(int nRows);

protected:
    virtual void	mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
    {
		QPointF p = pos();
        this->setSelected(true);
        emit changeSelected(this);
        this->update();
    }

    virtual void	hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
    {
        mIsStayed = true;
        this->update();
    }

    virtual void	hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
    {
        mIsStayed = false;
        this->update();
    }

public slots:
    void swapPixmap();
    void start();
    void stop();
signals:
    void changeSelected(DPGraphicsItem* item);

    void animNormal();
    void animLeft();
    void animRight();
    void animUp();
    void animDown();

    void animLeftBack();
    void animRightBack();
    void animUpBack();
    void animDownBack();

    void animDisappear();

    void disappear();

    void animFall();

    void fall();

private:
    int      mPixmapIndex;
    int      mWillChangeIndex;
    bool     mIsStayed;
    bool     mIsStop;

    QPoint   mCoord;

    QStateMachine*  mStateMachine;
    QState*         mNormal;
    QState*         mLeft;
    QState*         mRight;
    QState*         mUp;
    QState*         mDown;
    QState*         mLeftBack;
    QState*         mRightBack;
    QState*         mUpBack;
    QState*         mDownBack;

    QState*              mTmpState;
    QState*              mDisappearState;
    QAbstractTransition* mTmpTrans;
};
#endif // DPGRAPHICSITEM_H
